package com.tanhua.server.service;

import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.dubbo.api.db.BlackListApi;
import com.tanhua.dubbo.api.db.QuestionApi;
import com.tanhua.dubbo.api.db.SettingApi;
import com.tanhua.server.interceptors.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {

    @Reference
    private QuestionApi questionApi;
    @Reference
    private SettingApi settingApi;
    @Reference
    private BlackListApi blackListApi;

    public ResponseEntity querySettings() {
//        app想要的数据 SettingsVo 包含 用户手机号 陌生人问题 通知

        String mobile = UserHolder.getUser().getMobile();

        Question question = questionApi.findByUserId(UserHolder.getUserId());

        Settings settings = settingApi.findByUserId(UserHolder.getUserId());

        SettingsVo settingsVo = new SettingsVo();
        settingsVo.setId(UserHolder.getUserId());
        settingsVo.setPhone(mobile);
        if(question!=null){
            settingsVo.setStrangerQuestion(question.getTxt());
        }
        if(settings!=null){
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
        }
        return ResponseEntity.ok(settingsVo);
    }

    public ResponseEntity saveQuestions(String content) {
//        保存陌生人问题
        Question question = questionApi.findByUserId(UserHolder.getUserId());
        if(question==null){  //之前没有设置陌生人问题 所以要新增
            question = new Question();
            question.setUserId(UserHolder.getUserId());
            question.setTxt(content);
            questionApi.save(question);
        }else{
            question.setTxt(content);
            questionApi.update(question);
        }

        return ResponseEntity.ok(null);

    }

    public ResponseEntity saveNotifications(Settings settings) {

        //        保存通知设置
        Settings settingsDb = settingApi.findByUserId(UserHolder.getUserId());
        if(settingsDb==null){  //之前没有设置陌生人问题 所以要新增
            settings.setUserId(UserHolder.getUserId());
            settingApi.save(settings);
        }else{
            settings.setId(settingsDb.getId());
            settingApi.update(settings);
        }

        return ResponseEntity.ok(null);

    }

    public ResponseEntity queryBlacklist(Integer page, Integer pagesize) {

      PageResult pageResult =  blackListApi.queryBlacklist(UserHolder.getUserId(),page,pagesize);

      return ResponseEntity.ok(pageResult);

    }

    public ResponseEntity deleteBlacklist(Long blackUserId) {
//        DELETE FROM tb_black_list WHERE black_user_id=20 AND user_id=??

        blackListApi.deleteBlacklist(blackUserId,UserHolder.getUserId());

        return ResponseEntity.ok(null);
    }
}
